<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit">
    <a-form :form="form">
      <a-form-item style="display:none"><a-input v-decorator="['teacherId']" /></a-form-item>
      <a-form-item style="display:none"><a-input v-decorator="['deptName']" /></a-form-item>
      <a-row :gutter="48">
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="工号"><a-input placeholder="工号" v-decorator="['jobNumber']" /></a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="教师姓名">
            <a-input placeholder="教师姓名" v-decorator="['name', { rules: [{ required: true, message: '请输入教师姓名' }] }]" />
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="身份证号"><a-input placeholder="身份证号" v-decorator="['idNumber']" /></a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="性别">
            <a-select placeholder="请选择性别" v-decorator="['gender']" allowClear>
              <a-select-option v-for="(b, index) in genderList" :key="index" :value="b.dictValue">{{ b.dictLabel }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="出生日期">
            <a-date-picker valueFormat="YYYY-MM-DD" style="width: 100%;" v-decorator="['birthday']"></a-date-picker>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="民族">
            <a-select placeholder="请选择民族" v-decorator="['nation']" allowClear>
              <a-select-option v-for="(b, index) in nationList" :key="index" :value="b.dictValue">{{ b.dictLabel }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="籍贯"><a-input placeholder="籍贯" v-decorator="['nativePlace']" /></a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="政治面貌">
            <a-select placeholder="请选择政治面貌" v-decorator="['politicalStatus']" allowClear>
              <a-select-option v-for="(b, index) in politicalStatusList" :key="index" :value="b.dictValue">{{ b.dictLabel }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="电子邮件"><a-input placeholder="电子邮件" v-decorator="['email']" /></a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="电话号码"><a-input placeholder="电话号码" v-decorator="['telphone']" /></a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="职称">
            <a-select placeholder="请选择职称" v-decorator="['jobTitle']" allowClear>
              <a-select-option v-for="(b, index) in jobTitleList" :key="index" :value="b.dictValue">{{ b.dictLabel }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="职称时间">
            <a-date-picker valueFormat="YYYY-MM-DD" style="width: 100%;" v-decorator="['jobTime']"></a-date-picker>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="聘任岗位"><a-input placeholder="聘任岗位" v-decorator="['appoTitle']" /></a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="聘任时间">
            <a-date-picker valueFormat="YYYY-MM-DD" style="width: 100%;" v-decorator="['appoTime']"></a-date-picker>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="部门">
            <a-tree-select
              v-decorator="['deptId', { rules: [{ required: true, message: '请选择部门' }] }]"
              :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
              :treeData="deptTree"
              @change="onChange"
              placeholder="部门"
              treeDefaultExpandAll
            ></a-tree-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24"></a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>
<script>
import { saveTcTeacher } from '@/api/teacher'
import pick from 'lodash.pick'
export default {
  name: 'TeacherModal',
  props: {
    deptTree: {
      type: Array,
      required: true
    },
    genderList: {
      type: Array,
      required: true
    },
    nationList: {
      type: Array,
      required: true
    },
    politicalStatusList: {
      type: Array,
      required: true
    },
    jobTitleList: {
      type: Array,
      required: true
    }
  },
  components: {},
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {},
  created () {},
  methods: {
    add () {
      this.form.resetFields()
      this.edit({ teacherId: 0 })
    },
    edit (record) {
      this.mdl = Object.assign(record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.mdl,
            'teacherId',
            'jobNumber',
            'name',
            'gender',
            'nation',
            'birthday',
            'idNumber',
            'nativePlace',
            'politicalStatus',
            'email',
            'telphone',
            'deptName',
            'deptId',
            'jobTitle',
            'jobTime',
            'appoTitle',
            'appoTime'
          )
        )
      })
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveTcTeacher(values)
            .then(res => {
              if (res.code === 0) {
                this.$message.success('保存成功')
                this.$emit('ok')
                this.visible = false
              } else {
                this.$message.error(res.msg)
              }
            })
            .catch(() => {
              this.$message.error('系统错误，请稍后再试')
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    onChange (value, label, extra) {
      this.$nextTick(() => {
        this.form.setFieldsValue({
          deptName: label[0]
        })
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
